package cn.yougou.controller;


import cn.yougou.common.DtoUtil;
import cn.yougou.dto.Dto;
import cn.yougou.pojo.YgCommoditySpecification;
import cn.yougou.service.shopcart.YgCommoditySpecificationService;
import cn.yougou.vo.YgCommoditySpecificationVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * (YgCommoditySpecification)表控制层
 *
 * @author 白杰崇
 * @since 2020-12-11 18:57:46
 */
@RestController
@RequestMapping("ygCommoditySpecification")
public class YgCommoditySpecificationController {
    /**
     * 服务对象
     */
    @Reference
    private YgCommoditySpecificationService ygCommoditySpecificationService;

    /**
     * 查询商品所有规格参数（加入购物车/点击类型）
     * @return
     */
    @GetMapping("/specificationAll")
    public Dto specificationAll(Integer commodityid){
        Dto dto = null;
        if(commodityid == null){
            return DtoUtil.returnFail("缺少必要的参数","50001");
        }
        try{
            YgCommoditySpecification ygCommoditySpecification = new YgCommoditySpecification(commodityid);
            List<YgCommoditySpecificationVo> list =  ygCommoditySpecificationService.spAll(ygCommoditySpecification);
            if(list.isEmpty()){
                dto = DtoUtil.returnFail("暂无商品上架","50000");
            }
            dto = DtoUtil.returnDataSuccess(list);
        }catch (Exception e){
            e.printStackTrace();
            dto = DtoUtil.returnFail("系统错误！","49999");
        }
        return dto;
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public YgCommoditySpecification selectOne(Integer id) {
        return this.ygCommoditySpecificationService.queryById(id);
    }

}